{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.12 \u5bfc\u5165\u6a21\u5757\u7684\u540c\u65f6\u4fee\u6539\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u7ed9\u67d0\u4e2a\u5df2\u5b58\u5728\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6dfb\u52a0\u88c5\u9970\u5668\u3002\n\u4e0d\u8fc7\uff0c\u524d\u63d0\u662f\u8fd9\u4e2a\u6a21\u5757\u5df2\u7ecf\u88ab\u5bfc\u5165\u5e76\u4e14\u88ab\u4f7f\u7528\u8fc7\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u95ee\u9898\u7684\u672c\u8d28\u5c31\u662f\u4f60\u60f3\u5728\u6a21\u5757\u88ab\u52a0\u8f7d\u65f6\u6267\u884c\u67d0\u4e2a\u52a8\u4f5c\u3002\n\u53ef\u80fd\u662f\u4f60\u60f3\u5728\u4e00\u4e2a\u6a21\u5757\u88ab\u52a0\u8f7d\u65f6\u89e6\u53d1\u67d0\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u901a\u77e5\u4f60\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4f7f\u752810.11\u5c0f\u8282\u4e2d\u540c\u6837\u7684\u5bfc\u5165\u94a9\u5b50\u673a\u5236\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u65b9\u6848\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# postimport.py\nimport importlib\nimport sys\nfrom collections import defaultdict\n\n_post_import_hooks = defaultdict(list)\n\nclass PostImportFinder:\n    def __init__(self):\n        self._skip = set()\n\n    def find_module(self, fullname, path=None):\n        if fullname in self._skip:\n            return None\n        self._skip.add(fullname)\n        return PostImportLoader(self)\n\nclass PostImportLoader:\n    def __init__(self, finder):\n        self._finder = finder\n\n    def load_module(self, fullname):\n        importlib.import_module(fullname)\n        module = sys.modules[fullname]\n        for func in _post_import_hooks[fullname]:\n            func(module)\n        self._finder._skip.remove(fullname)\n        return module\n\ndef when_imported(fullname):\n    def decorate(func):\n        if fullname in sys.modules:\n            func(sys.modules[fullname])\n        else:\n            _post_import_hooks[fullname].append(func)\n        return func\n    return decorate\n\nsys.meta_path.insert(0, PostImportFinder())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528 when_imported() \u88c5\u9970\u5668\u4e86\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from postimport import when_imported\n@when_imported('threading')\ndef warn_threads(mod):\n    print('Threads? Are you crazy?')\nimport threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u66f4\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u60f3\u5728\u5df2\u5b58\u5728\u7684\u5b9a\u4e49\u4e0a\u9762\u6dfb\u52a0\u88c5\u9970\u5668\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\nfrom postimport import when_imported\n\ndef logged(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        print('Calling', func.__name__, args, kwargs)\n        return func(*args, **kwargs)\n    return wrapper\n\n# Example\n@when_imported('math')\ndef add_logging(mod):\n    mod.cos = logged(mod.cos)\n    mod.sin = logged(mod.sin)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6280\u672f\u4f9d\u8d56\u4e8e10.11\u5c0f\u8282\u4e2d\u8bb2\u8ff0\u8fc7\u7684\u5bfc\u5165\u94a9\u5b50\uff0c\u5e76\u7a0d\u4f5c\u4fee\u6539\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "@when_imported \u88c5\u9970\u5668\u7684\u4f5c\u7528\u662f\u6ce8\u518c\u5728\u5bfc\u5165\u65f6\u88ab\u6fc0\u6d3b\u7684\u5904\u7406\u5668\u51fd\u6570\u3002\n\u8be5\u88c5\u9970\u5668\u68c0\u67e5sys.modules\u6765\u67e5\u770b\u6a21\u5757\u662f\u5426\u771f\u7684\u5df2\u7ecf\u88ab\u52a0\u8f7d\u4e86\u3002\n\u5982\u679c\u662f\u7684\u8bdd\uff0c\u8be5\u5904\u7406\u5668\u88ab\u7acb\u5373\u8c03\u7528\u3002\u4e0d\u7136\uff0c\u5904\u7406\u5668\u88ab\u6dfb\u52a0\u5230 _post_import_hooks \u5b57\u5178\u4e2d\u7684\u4e00\u4e2a\u5217\u8868\u4e2d\u53bb\u3002\n_post_import_hooks \u7684\u4f5c\u7528\u5c31\u662f\u6536\u96c6\u6240\u6709\u7684\u4e3a\u6bcf\u4e2a\u6a21\u5757\u6ce8\u518c\u7684\u5904\u7406\u5668\u5bf9\u8c61\u3002\n\u4e00\u4e2a\u6a21\u5757\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u5904\u7406\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8ba9\u6a21\u5757\u5bfc\u5165\u540e\u89e6\u53d1\u6dfb\u52a0\u7684\u52a8\u4f5c\uff0cPostImportFinder \u7c7b\u88ab\u8bbe\u7f6e\u4e3asys.meta_path\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\n\u5b83\u4f1a\u6355\u83b7\u6240\u6709\u6a21\u5757\u5bfc\u5165\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e2d\u7684 PostImportFinder \u7684\u4f5c\u7528\u5e76\u4e0d\u662f\u52a0\u8f7d\u6a21\u5757\uff0c\u800c\u662f\u81ea\u5e26\u5bfc\u5165\u5b8c\u6210\u540e\u89e6\u53d1\u76f8\u5e94\u7684\u52a8\u4f5c\u3002\n\u5b9e\u9645\u7684\u5bfc\u5165\u88ab\u59d4\u6d3e\u7ed9\u4f4d\u4e8esys.meta_path\u4e2d\u7684\u5176\u4ed6\u67e5\u627e\u5668\u3002\nPostImportLoader \u7c7b\u4e2d\u7684 imp.import_module() \u51fd\u6570\u88ab\u9012\u5f52\u7684\u8c03\u7528\u3002\n\u4e3a\u4e86\u907f\u514d\u9677\u5165\u65e0\u7ebf\u5faa\u73af\uff0cPostImportFinder \u4fdd\u6301\u4e86\u4e00\u4e2a\u6240\u6709\u88ab\u52a0\u8f7d\u8fc7\u7684\u6a21\u5757\u96c6\u5408\u3002\n\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u540d\u5b58\u5728\u5c31\u4f1a\u76f4\u63a5\u88ab\u5ffd\u7565\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab imp.import_module() \u52a0\u8f7d\u540e\uff0c\n\u6240\u6709\u5728_post_import_hooks\u88ab\u6ce8\u518c\u7684\u5904\u7406\u5668\u88ab\u8c03\u7528\uff0c\u4f7f\u7528\u65b0\u52a0\u8f7d\u6a21\u5757\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\u672c\u673a\u4e0d\u9002\u7528\u4e8e\u90a3\u4e9b\u901a\u8fc7 imp.reload() \u88ab\u663e\u5f0f\u52a0\u8f7d\u7684\u6a21\u5757\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u52a0\u8f7d\u4e00\u4e2a\u4e4b\u524d\u5df2\u88ab\u52a0\u8f7d\u8fc7\u7684\u6a21\u5757\uff0c\u90a3\u4e48\u5bfc\u5165\u5904\u7406\u5668\u5c06\u4e0d\u4f1a\u518d\u88ab\u89e6\u53d1\u3002\n\u53e6\u5916\uff0c\u8981\u662f\u4f60\u4ecesys.modules\u4e2d\u5220\u9664\u6a21\u5757\u7136\u540e\u518d\u91cd\u65b0\u5bfc\u5165\uff0c\u5904\u7406\u5668\u53c8\u4f1a\u518d\u4e00\u6b21\u89e6\u53d1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u5173\u4e8e\u5bfc\u5165\u540e\u94a9\u5b50\u4fe1\u606f\u8bf7\u53c2\u8003 PEP 369."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}